﻿using Google.Protobuf.WellKnownTypes;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
public class VisistsPanelData
{
    public List<Visit> visitors;
    public List<Visit> myVisits;
}

public class VisitsPanel : AppUI
{
    List<VisitorItem> m_VisitorItems = new List<VisitorItem>();
    public Transform visitorItemsParent;
    public VisitorItem visitorItemPref;

    List<MyVisitItem> m_MyVisits = new List<MyVisitItem>();
    public Transform myVisitItemsParent;
    public MyVisitItem myVisitItemPref;

    public GameObject visitorItemsObj;
    public GameObject myVisitItemsObj;

    VisistsPanelData m_Data;

    public static int lastSelectToggle = 1;

    public const int Toggle_Visitors = 1;
    public const int Toggle_MyVisits = 2;

    public Text totalVisitCount;
    public Text todayVisitCount;
    public ListTogglesGroup togglesGroup;
    public ListTogglesGroup myVisitDateGroup;

    public override void Start()
    {
        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);

        visitorItemPref.gameObject.SetActive(false);
        myVisitItemPref.gameObject.SetActive(false);
    }

    public void Toggle(int id)
    {
        lastSelectToggle = id;

        if (togglesGroup)
            togglesGroup.Init(OnToggleSelect, lastSelectToggle);
    }

    void OnToggleSelect(int id)
    {
        lastSelectToggle = id;

        if (id == Toggle_Visitors)
        {
            visitorItemsObj.gameObject.SetActive(true);
            myVisitItemsObj.gameObject.SetActive(false);
        }
        else if (id == Toggle_MyVisits)
        {
            visitorItemsObj.gameObject.SetActive(false);
            myVisitItemsObj.gameObject.SetActive(true);
        }
    }

    public void Init(VisistsPanelData data)
    {
        m_Data = data;

        this.totalVisitCount.text = data.visitors.Count + "人看过你";
        
        DateTime today = UnityFunctions.GetTimeZoneNowDateTime();
        this.todayVisitCount.text = "今日访客" + data.visitors.AllWhere(e =>
        {
            var visitDate = UnityFunctions.GetTimeZoneDateTime(e.lastVisitTime);
            return visitDate.Day == today.Day
            && visitDate.Month == today.Month
            && visitDate.Year == today.Year;

        }).Count();

        UnityFunctions.ClearList(m_VisitorItems);

        for (int i = 0; i < data.visitors.Count; i++)
        {
            var visitor = data.visitors[i];
            var newItem = Instantiate(visitorItemPref, visitorItemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(visitor, OnSelectVisitorItem);

            m_VisitorItems.Add(newItem);
        }

        for(int i = 8; i >= 1; i--)
        {
            var item = this.myVisitDateGroup.toggleItems[i - 1];
            var itemDayOfWeek = item.transform.Find("DayOfWeek").GetComponent<Text>();
            var itemDay = item.transform.Find("DayText").GetComponent<Text>();

            DateTime targetDate = UnityFunctions.GetTimeZoneNowDateTime().AddDays(i - 8);

            if (itemDayOfWeek && itemDay)
            {
                if (i == 8)
                    itemDayOfWeek.text = "今";
                else 
                    itemDayOfWeek.text = DataString.GetDayOfWeekStr(targetDate.DayOfWeek).Replace("星期","");

                itemDay.text = targetDate.Day.ToString();
            }    
        }

        myVisitDateGroup.Init(OnSelectMyVisitDate, 8);

        StartCoroutine(UnityFunctions.AdjustItemsLayout(transform));
    }

    void OnSelectMyVisitDate(int id)
    {
        List<Visit> myVisits = m_Data.myVisits;

        DateTime targetDate = UnityFunctions.GetTimeZoneNowDateTime().AddDays(id - 8);

        GameDebug.Log($"curTimestamp:{UnityFunctions.GetTimeStampSeconds()} myVisits:{myVisits.Count}");
        Visit[] toShow = myVisits.AllWhere(e =>
            {
                var visitDate = UnityFunctions.GetTimeZoneDateTime(e.lastVisitTime);
                GameDebug.Log($"lastVisitTime:{visitDate.Month} - {visitDate.Day} ");

                return visitDate.Day == targetDate.Day
                && visitDate.Month == targetDate.Month
                && visitDate.Year == targetDate.Year;

            }).AsArray();
        
        GameDebug.Log($"OnSelectMyVisitDate:{targetDate.Month} - {targetDate.Day}, myVisits:{myVisits.Count} toShow:{toShow.Length}  ");

        UnityFunctions.ClearList(m_MyVisits);

        for (int i = toShow.Length - 1; i >= 0; i--)
        {
            var myVisit = toShow[i];
            var newItem = Instantiate(myVisitItemPref, myVisitItemsParent);
            newItem.gameObject.SetActive(true);
            newItem.Init(myVisit, OnSelectMyVisitItem);

            m_MyVisits.Add(newItem);
        }
    }

    public void OnSelectMyVisitItem(MyVisitItem item)
    {
        var userHome = this.GetComponentInParent<UIController>().ShowUI<UserHomePanel>();

        RequestServerController.instance.RequestUserMomentInfo(item.data.userId, (userInfo) =>
        {
            userHome.Init(userInfo);
        });

        RequestServerController.instance.ReqVisit(MGame.instance.MUser.id, item.data.userId, (v) =>
        {
            GameDebug.Log($"ReqVisit {item.data.userId} end");
        });
    }
    public void OnSelectVisitorItem(VisitorItem item)
    {
        var userHome = this.GetComponentInParent<UIController>().ShowUI<UserHomePanel>();

        RequestServerController.instance.RequestUserMomentInfo(item.data.userId, (userInfo) =>
        {
            userHome.Init(userInfo);
        });
        
        RequestServerController.instance.ReqVisit(MGame.instance.MUser.id, item.data.userId, (v) =>
        {
            GameDebug.Log($"ReqVisit {item.data.userId} end");
        });
    }
    // Update is called once per frame
    void Update()
    {

    }

    //public override void GoBack()
    //{
    //    UIController.ShowUI<MainPanel>();
    //}
}
